package com.shoppingmall.controller.portal;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.pojo.User;
import com.shoppingmall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 管理用户前端显示的购物车模块
 * Created by
 * @author touni
 * @date 2019/3/20
 * @time 10:11
 */
@RestController
@RequestMapping("/cart")
public class CartController {


    /**
     * 存储用户的session的名称
     */
    @Value("${USER_SESSION}")
    private String userSessoin;


    @Autowired
    private CartService cartService;

    /**
     *
     * 根据用户的id查询购物车
     * @param session
     * @return
     */
    @RequestMapping("/list")
    public ServletResponse getCartList(HttpSession session){
        User userFromSession = (User)session.getAttribute(userSessoin);
        if(null == userFromSession){
            return ServletResponse.createByErrorMessage("用户未登录!!!!");
        }
        return cartService.getCartList(userFromSession.getId());
    }


    /**
     * 用于向购物车增加或者修改商品 对应两个URL
     * tip 这样会导致可以通过add来进行update操作 ，相反也可以通过update进行add操作
     * @param session
     * @param productId
     * @param count
     * @return
     */
    @RequestMapping(value = {"/add","/update"})
    public ServletResponse addOrUpdateProductToCart(HttpSession session,Integer productId,Integer count){
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage("用户未登录！！！");
        }
        //判断参数是否合法
        if (null == productId || null == count){
            return ServletResponse.createByErrorMessage("哎呀网络有问题！！！请检查您的网络！！！");
        }
        return cartService.addOrUpdateProductToCart(userFromSession.getId(),productId,count);
    }

    /**
     * 传入多个productId进行删除
     * @param session
     * @param productIds
     * @return
     */
    @RequestMapping("/delete_product")
    public ServletResponse deleteProductFromCart(HttpSession session,String productIds){
        //判断用户是否登录
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage("用户未登录 请先登录哦！！！！");
        }
        //验证参数  一般需要用正则表达式匹配参数是否合法
        if (null == productIds){
            return ServletResponse.createByErrorMessage("抱歉，您删除的商品不存在");
        }
        return cartService.deleteProductFromCart(userFromSession.getId(),productIds);
    }


    /**
     * 购物车选中某个商品
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping("/select")
    public ServletResponse selectOneProduct(HttpSession session,Integer productId){
        //判断用户是否登录
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage(" 请先登录哦！！！！");
        }
        //验证参数  一般需要用正则表达式匹配参数是否合法
        if (null == productId){
            return ServletResponse.createByErrorMessage("抱歉，您选择的商品不存在");
        }
        return cartService.selectOneProduct(userFromSession.getId(),productId);

    }

    @RequestMapping("/un_select")
    public ServletResponse unselectOneProduct(HttpSession session,Integer productId){
        //判断用户是否登录
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage(" 请先登录哦！！");
        }
        //验证参数
        if (null == productId){
            return ServletResponse.createByErrorMessage("抱歉，您选择的商品不存在");
        }
        return cartService.unselectOneProduct(userFromSession.getId(),productId);
    }




    @RequestMapping("/get_cart_product_count")
    public ServletResponse getCartProductCount(HttpSession session){
        //判断用户是否登录
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage(" 请先登录哦！！");
        }

        return cartService.getCartProductCount(userFromSession.getId());

    }


    @RequestMapping("/select_all")
    public ServletResponse selectAllProduct(HttpSession session){
        //判断用户是否登录
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage(" 请先登录哦！！");
        }

        return cartService.selectAllProduct(userFromSession.getId());

    }


    @RequestMapping("/un_select_all")
    public ServletResponse unselectAllProduct(HttpSession session){
        //判断用户是否登录
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession){
            return ServletResponse.createByErrorMessage(" 请先登录哦！！！");
        }

        return cartService.unselectAllProduct(userFromSession.getId());

    }
























}
